const ua = window.navigator.userAgent
export const browser = {
  trident: ua.indexOf('Trident') > -1, // IE内核
  presto: ua.indexOf('Presto') > -1, // opera内核
  webKit: ua.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
  gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, // 火狐内核
  mobile: !!ua.match(/AppleWebKit.*Mobile/i) || !!ua.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TLC|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HATER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), // 是否为移动终端
  ios: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
  android: ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1, // android终端或uc浏览器
  iPhone: ua.indexOf('iPhone') > -1 || ua.indexOf('Mac') > -1, // 是否为iphone或者QQ HD浏览器
  iPad: ua.indexOf('iPad') > -1, // 是否iPad终端
  webApp: ua.indexOf('Safari') === -1, // 是否web应用程序，没有头部与底部
  /* eslint-disable */
  weixin: ua.toLowerCase().match(/MicroMessenger/i) == 'micromessenger', // 微信浏览器
  /* eslint-disable */
  isApp: ua.indexOf('thinkive') > -1, // 是掌证宝app
  isUpSdk: ua.indexOf('UPHybridSDK') > -1, // 是掌证宝app
  // 浏览器语言
  language: (window.navigator.browserLanguage || window.navigator.language).toLowerCase()
}

//ws发送消息
export function wsSend(ws, data) {
  let msgBody = {
    type: data.contentType,
    sTime: '',
    serial: '',
    content: data.content
  }
  const params = JSON.stringify(msgBody)
  ws.send(params)
}

//  返回消息消息体格式
export function msgBody(data) {
  return {
    meetingId: data.meetingId,
    user: {
      memberId: data.memberId,
      memberType: 'member'
    },
    kf: {
      waiterId: data.waiterId,
      waiterType: 'waiter'
    },
    contentType: data.contentType || 8,
    content: data.content || ''
  }
}

//显示下聊天窗口的文本消息体封装
export function messageBody(params) {
  return {
    type: params.type, //'system'| 'user'|'link'
    msg: {
      id: params.id, //消息id（主键） 随机生成
      type: params.talkType, //"text"|"voice"|"img"|"link"
      time: params.time,
      userinfo: {
        uid: params.uid, //用户id
        username: '客服小哥'
        // face: "/static/img/face.jpg",  //用户图像
      },
      content: params.content
      // content: {
      // 	text: txt, //key为text表示文本, 为url表示图片或语音地址
      // },
    }
  }
}

/* 类型判断 */
export function checkType(data) {
  // 用来判断被拷贝对象数据类型的函数
  return Object.prototype.toString.call(data).slice(8, -1)
}
/**
 * @param {any} source
 * @returns {Object}
 */

export function deepClone(source) {
  // 实现深度拷贝的函数
  let result,
    sourceType = checkType(source)
  if (sourceType === 'Object') {
    // 只有对象和数组才进行深度拷贝
    result = {}
  } else if (sourceType === 'Array') {
    result = []
  } else {
    return source
  }
  for (let i in source) {
    // i为对象的key或数组的下标
    let value = source[i]
    let valueType = checkType(value)
    if (valueType === 'Object' || valueType === 'Array') {
      // 当拷贝的对象中还有数组或是对象时进行递归，克隆它们中的内容，直到递归完所有数组或对象为止
      result[i] = deepClone(value)
    } else {
      result[i] = value
    }
  }
  return result // 返回最终的结果
}

/**
 * @param {Object} FirstOBJ,SecondOBJ
 * @returns {Object}
 */
export function deepAssign(FirstOBJ, SecondOBJ) {
  // 深度合并对象
  for (var key in SecondOBJ) {
    FirstOBJ[key] = FirstOBJ[key] && FirstOBJ[key].toString() === '[object Object]' ? deepAssign(FirstOBJ[key], SecondOBJ[key]) : (FirstOBJ[key] = SecondOBJ[key])
  }
  return FirstOBJ
}

// 历史查询时间格式化 (yyyy-mm-ddTHH:mm:ss)
// 返回[当前时间，一年后的当前时间]
export function recoderTimeformat(date) {
  if (!date || !(date instanceof Date)) {
    date = new Date()
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const ducMoth = month - 1
  let start
  if (ducMoth > 0) {
    start = `${year}-${ducMoth}-${day}T${hour}:${minute}:${second}`
  } else if (ducMoth === 0) {
    start = `${year - 1}-12-${day}T${hour}:${minute}:${second}`
  } else {
    const _month = 12 + ducMoth
    start = `${year - 1}-${_month}-${day}T${hour}:${minute}:${second}`
  }
  const end = `${year}-${month}-${day}T${hour}:${minute}:${second}`
  return [start, end]
}

export function dataURLtoFile(dataurl, filename) {
  // let arr = dataurl.split(',');
  // let mime = arr[0].match(/:(.*?);/)[1];
  let bstr = atob(dataurl)
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  //转换成file对象
  return new File([u8arr], filename, { type: 'image/jpeg' })
  //转换成成blob对象
  //return new Blob([u8arr],{type:mime});
}

export function addZero(data, len) {
  return (Array(len).join('0') + data).slice(-len)
}

//格式化后端端时间戳
//格式化后端端时间戳
export function transTimestamp(timestamp) {
  if (!timestamp) {
    return ''
  }
  let date = new Date(timestamp)
  let now = new Date()
  // const nowyear = now.getFullYear();
  const nowmonth = now.getMonth() + 1
  const nowday = now.getDate()
  // const year = date.getFullYear();
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds();
  if (nowmonth == month && nowday == day) {
    return `今日 ${addZero(hour, 2)}:${addZero(minute, 2)}`
  }
  if (nowmonth == 1 && nowday === 1 && month == 12 && day == 31) {
    return `昨日 ${addZero(hour, 2)}:${addZero(minute, 2)}`
  }
  if (nowmonth == month && nowday - day == 1) {
    return `昨日 ${addZero(hour, 2)}:${addZero(minute, 2)}`
  }
  return `${addZero(month, 2)}月${addZero(day, 2)}日 ${addZero(hour, 2)}:${addZero(minute, 2)}`
}
